﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BskLeague.Models;
using System.Web.Security;
using BskLeague.ViewModels;

namespace BskLeague.Controllers
{
    public class ScheduleGeneratorController : Controller
    {
        BskLeagueDB _db = new BskLeagueDB();

        static ScheduleGenerator sg = new ScheduleGenerator();
        //
        // GET: /ScheduleGenerator/

        public ActionResult Index()
        {
            ScheduleGeneratorTeamsViewModel sgtvm = new ScheduleGeneratorTeamsViewModel();
            sgtvm.teams = _db.Teams.ToList();
            correct = true;
            if (sg != null)
            {
                sg.Terms.Clear();

            }
            Season actualSeason;
            if (_db.Seasons.Any(s => s.StatusId == 1))
            {
                actualSeason = _db.Seasons.Single(s => s.StatusId == 1);
            }
            else
            {

                return RedirectToAction("ManageSeasons","Admin");
            }
            int actualSezonId = actualSeason.SeasonId;
            sgtvm.teams = (from p in _db.Teams
                           where p.STPs.Any(a => a.SeasonId == actualSezonId)
                           select p).ToList();
            sgtvm.check= new List<bool>();
            for (int i = 0; i < sgtvm.teams.Count(); i++)
            {
                sgtvm.check.Add(true);
            }
            return View(sgtvm);
        }


        [HttpPost]
        public ActionResult Index(ScheduleGeneratorTeamsViewModel sgtvm)
        {
            sg.Teams.Clear();

            for (int i = 0; i < sgtvm.teams.Count; i++)
            {
                if (sgtvm.check[i]==true)
                {
                    sg.Teams.Add(sgtvm.teams[i]);
                }
            }
            if (sg.Teams.Count % 2 == 0)
            {
                sg.matchesNeeded = (sg.Teams.Count - 1) * (sg.Teams.Count / 2);
            }
            else
            {
                sg.matchesNeeded = (sg.Teams.Count) * ((sg.Teams.Count-1) / 2);
            }

            
            return RedirectToAction("Terms");
        }

        public ActionResult Terms()
        {
            
            if (correct)
            {
                sg.Terms = new List<AvaibleTerm>();
                ViewBag.MessageToClient = "Do wygenerowania terminarza potrzebne jest: " + sg.matchesNeeded + " terminów.";
            }else
            {
                ViewBag.MessageToClient = "Za mało terminów! Potrzeba " + sg.matchesNeeded+" terminów.";
            }
            List<SelectListItem> items = new List<SelectListItem>();

            foreach (Stadium s in _db.Stadiums)
            {
                items.Add(new SelectListItem { Text = s.stadiumName, Value = s.stadiumId.ToString() });
            }

            ViewBag.stadiumId = items;
            return View(sg.Terms);
        }
        static bool correct = true;
        public ActionResult Generate()
        {
            if (sg.Terms.Count < sg.matchesNeeded)
            {
                correct = false;
                return RedirectToAction("Terms");
            }
            return View(sg.GenerateSchedule());
        }

        [HttpPost]
        public ActionResult Create(AvaibleTerm term)
        {
            term.stadiumName=_db.Stadiums.ToList().Find(a => a.stadiumId == term.stadiumId).stadiumName;
            if (ModelState.IsValid)
            {
                sg.Terms.Add(term);
            }

            return PartialView("AvaibleTerms", sg.Terms);
        }

        public ActionResult AcceptSchedulde()
        {
            sg.WriteToDb();
            return View();
        }

       [HttpPost]
        public ActionResult Delete(int id)
        {
            sg.Terms.RemoveAt(id - 1);
            return PartialView("AvaibleTerms", sg.Terms);
        }

    }
}
